package com.example.administrator.bitmaputils;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.ArrayList;

public class Main2Activity extends Activity implements View.OnClickListener {
    GridView gv;
    BaseAdapter gvAdapter;
    ImageView mCamera;
    ArrayList<PictureInfo> mImgs = new ArrayList<PictureInfo>();
    private Handler mHandler = new Handler();
    private ImageCache mCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mCamera = (ImageView) LayoutInflater.from(this).
                inflate(R.layout.griditem_select_topic_img_capture, null);
        mCamera.setOnClickListener(this);
        loadImageBack();
        initCache();
        gv = (GridView) findViewById(R.id.gv);
        gvAdapter = new BaseAdapter() {

            @Override
            public int getCount() {
                return mImgs.size() + 1;
            }

            @Override
            public Object getItem(int position) {
                return mImgs.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (position == 0) {
                    return mCamera;
                } else if (convertView == mCamera) {
                    convertView = null;
                }
                position -= 1;
                convertView = bindImgeView(position, convertView);

                return convertView;
            }

        };
        gv.setAdapter(gvAdapter);
    }

    private void initCache() {
        mCache = new ImageCache() {
            protected Context getContext() {
                return Main2Activity.this;
            }

            protected Handler getHandler() {
                return mHandler;
            }
        };
    }

    private class ImgViewTag {
        ImageView mImg;
        ImageView mChecked;

        public ImgViewTag(View root) {
            mImg = (ImageView) root.findViewById(R.id.img);
            mChecked = (ImageView) root.findViewById(R.id.checked);
        }
    }

    public PictureInfo createPictureInfo(Cursor cursor) {
        PictureInfo picture = new PictureInfo();
        picture.size = cursor.getInt(cursor.getColumnIndex("_size"));
        picture.path = cursor.getString(cursor.getColumnIndex("_data"));
        picture.name = cursor.getString(cursor.getColumnIndex("_display_name"));
        picture.folder = cursor.getString(cursor.getColumnIndex("bucket_display_name"));
        picture.mimeType = cursor.getString(cursor.getColumnIndex("mime_type"));
        String orientation = cursor.getString(cursor.getColumnIndex("orientation"));
        if (orientation != null && !"".equals(orientation)) {
            picture.degree = Integer.parseInt(orientation);
        }
        return picture;
    }

    //加载手机中所有图片
    private void loadImageBack() {
        final ArrayList<PictureInfo> imgs = new ArrayList<PictureInfo>();
        Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if (cursor == null) {
            return;
        }
        while (cursor.moveToNext()) {
            PictureInfo picture = createPictureInfo(cursor);
            if (picture.size > 0) {
                imgs.add(picture);
            }
        }
        cursor.close();

        mHandler.post(new Runnable() {
            public void run() {
                mImgs = imgs;
                gvAdapter.notifyDataSetChanged();
            }
        });
    }

    private View bindImgeView(final int n, View v) {
        ImgViewTag tag;

        if (v == null) {
            v = LayoutInflater.from(Main2Activity.this).
                    inflate(R.layout.griditem_social_check_img, null);
            tag = new ImgViewTag(v);
            v.setTag(tag);
        } else {
            tag = (ImgViewTag) v.getTag();
        }

        final PictureInfo img = mImgs.get(n);
        mCache.Bind(tag.mImg, img.path);

        tag.mChecked.setSelected(img.isSelected);
        final ImgViewTag checked = tag;

        tag.mChecked.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                onCheckImg(checked, img);
            }
        });

        tag.mImg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                onPreview(n);
            }
        });

        return v;
    }

    @Override
    public void onClick(View v) {

    }
}
